<?php


namespace app\finance\controller;


use app\base\BaseController;
use app\finance\servers\BaseService;
use think\facade\Db;

class Commission extends BaseController
{
    protected $title='提成核算';
    protected $table='sale_target_detail';
        public function index()
    {
        $param=$this->request->param();
        if (request()->isAjax()) {
            $where[]=['std.status','=',2];

            if (isset($this->param['orderType'])&&$this->param['orderType']){
                $where[]=['std.orderType','=',$this->param['orderType']];
            }
            if (isset($this->param['mType'])&&$this->param['mType']){
                $where[]=['std.mType','=',$this->param['mType']];
            }
            if (isset($this->param['name'])&&$this->param['name']){
                $where[]=['py.name|py.code|ad.name|std.relatedOrderNum','like','%'.$this->param['name'].'%'];
            }
            if (isset($this->param['orderDate'])&&$this->param['orderDate']){
                $dates=explode('~',$this->param['orderDate']);
                $where[]=['std.orderDate','between',$dates];
            }
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $list = Db::name($this->table)
                ->alias('std')
                ->leftJoin('sale_target st','st.id=std.orderId')
                ->leftJoin('admin ad','ad.id=std.adminId')
                ->leftJoin('position pi','pi.id=ad.position_id')
                ->where($where)
                ->field('std.*,st.years,st.name as targetName,st.groupId,st.targets as groupTargets,ad.name as userName,ad.position_id,ad.did userDepartId,pi.title as positionName')
                ->order('st.years DESC,std.id DESC')
                ->paginate($rows)->each(function ($item){
                    $jan=0;
                    $feb=0;
                    $mar=0;
                    $apr=0;
                    $may=0;
                    $jun=0;
                    $jul=0;
                    $aug=0;
                    $sep=0;
                    $oct=0;
                    $nov=0;
                    $dec=0;
                    $overed=0;
                    $overedl=0;
                    $ndjx=0;
                    $nzjx=0;
                    $gljt=0;
                    if ($item['position_id']==9){
                        $item['dTargets']=0;
                        $ordernums=[];
                        $scList=Db::name('sale_contract')->where('status',4)
                            ->where('saleId',$item['adminId'])
                            ->where('type',1)
                            ->where('payStatus',3)
                            ->whereYear('payOverTime',$item['years'])
                            ->order('payOverTime ASC')
                            ->field('id,countMoney,orderNum,payOverTime')->select();
                        foreach ($scList as $v){
                            $ordernums[]=$v['orderNum'];
                            $mon=date('m',strtotime($v['payOverTime']));
                            $money=$v['countMoney'];
                            $overed+=$v['countMoney'];
                            switch ($mon){
                                case '01': $jan+=$money;  break;
                                case '02': $feb+=$money;   break;
                                case '03': $mar+=$money;   break;
                                case '04': $apr+=$money;   break;
                                case '05': $may+=$money;   break;
                                case '06': $jun+=$money;   break;
                                case '07': $jul+=$money;   break;
                                case '08': $aug+=$money;   break;
                                case '09': $sep+=$money;   break;
                                case '10':$oct+=$money;    break;
                                case '11':$nov+=$money;    break;
                                case '12': $dec+=$money;   break;
                            }
                        }
                        $cbs=Db::name('sale_contract_detail')
                            ->alias('scd')
                            ->leftJoin('sale_contract sc','sc.orderNum=scd.saleContractOrderNum')
                            ->where('scd.status',4)->where('scd.type',2)
                            ->whereYear('payOverTime',$item['years'])
                            ->where('saleContractOrderNum','in',$ordernums)
                            ->order('payOverTime ASC')
                            ->field('scd.bjMoney,sc.payOverTime')->select();
                        $jancb=0;
                        $febcb=0;
                        $marcb=0;
                        $aprcb=0;
                        $maycb=0;
                        $juncb=0;
                        $julcb=0;
                        $augcb=0;
                        $sepcb=0;
                        $octcb=0;
                        $novcb=0;
                        $deccb=0;
                        foreach ($cbs as $c){
                            $mon=date('m',strtotime($c['payOverTime']));
                            switch ($mon){
                                case '01': $jancb+=$c['bjMoney'];  break;
                                case '02': $febcb+=$c['bjMoney'];   break;
                                case '03': $marcb+=$c['bjMoney'];   break;
                                case '04': $aprcb+=$c['bjMoney'];   break;
                                case '05': $maycb+=$c['bjMoney'];   break;
                                case '06': $juncb+=$c['bjMoney'];   break;
                                case '07': $julcb+=$c['bjMoney'];   break;
                                case '08': $augcb+=$c['bjMoney'];   break;
                                case '09': $sepcb+=$c['bjMoney'];   break;
                                case '10': $octcb+=$c['bjMoney'];    break;
                                case '11': $novcb+=$c['bjMoney'];    break;
                                case '12': $deccb+=$c['bjMoney'];   break;
                            }
                        }

                        $janll=$jan>0?($jan-$jancb)/$jan:0;
                        $febll=$feb>0?($feb-$febcb)/$feb:0;
                        $marll=$mar>0?($mar-$marcb)/$mar:0;
                        $aprll=$apr>0?($apr-$aprcb)/$apr:0;
                        $mayll=$may>0?($may-$maycb)/$may:0;
                        $junll=$jun>0?($jun-$juncb)/$jun:0;
                        $julll=$jul>0?($jul-$julcb)/$jul:0;
                        $augll=$aug>0?($aug-$augcb)/$aug:0;
                        $sepll=$sep>0?($sep-$sepcb)/$sep:0;
                        $octll=$oct>0?($oct-$octcb)/$oct:0;
                        $novll=$nov>0?($nov-$novcb)/$nov:0;
                        $decll=$dec>0?($dec-$deccb)/$dec:0;
                        $jan=$janll>0.05?round($jan*(0.03+($janll-0.05)*0.2),2):0;
                        $feb=$febll>0.05?round($feb*(0.03+($febll-0.05)*0.2),2):0;
                        $mar=$marll>0.05?round($mar*(0.03+($marll-0.05)*0.2),2):0;
                        $apr=$aprll>0.05?round($apr*(0.03+($aprll-0.05)*0.2),2):0;
                        $may=$mayll>0.05?round($may*(0.03+($mayll-0.05)*0.2),2):0;
                        $jun=$junll>0.05?round($jun*(0.03+($junll-0.05)*0.2),2):0;
                        $jul=$julll>0.05?round($jul*(0.03+($julll-0.05)*0.2),2):0;
                        $aug=$augll>0.05?round($aug*(0.03+($augll-0.05)*0.2),2):0;
                        $sep=$sepll>0.05?round($sep*(0.03+($sepll-0.05)*0.2),2):0;
                        $oct=$octll>0.05?round($oct*(0.03+($octll-0.05)*0.2),2):0;
                        $nov=$novll>0.05?round($nov*(0.03+($novll-0.05)*0.2),2):0;
                        $dec=$decll>0.05?round($dec*(0.03+($decll-0.05)*0.2),2):0;
                    }
                    elseif ($item['position_id']==10){
                        $item['dTargets']=0;
                        $ordernums=[];
                        $scList=Db::name('sale_contract')->where('status',4)
                            ->where('saleId',$item['adminId'])
                            ->where('type',1)
                            ->where('payStatus',3)
                            ->whereYear('payOverTime',$item['years'])
                            ->order('payOverTime ASC')
                            ->field('id,countMoney,orderNum,payOverTime')->select();
                        foreach ($scList as $v){
                            $ordernums[]=$v['orderNum'];
                            $mon=date('m',strtotime($v['payOverTime']));
                            $money=$v['countMoney'];
                            $overed+=$v['countMoney'];
                            switch ($mon){
                                case '01': $jan+=$money;  break;
                                case '02': $feb+=$money;   break;
                                case '03': $mar+=$money;   break;
                                case '04': $apr+=$money;   break;
                                case '05': $may+=$money;   break;
                                case '06': $jun+=$money;   break;
                                case '07': $jul+=$money;   break;
                                case '08': $aug+=$money;   break;
                                case '09': $sep+=$money;   break;
                                case '10':$oct+=$money;    break;
                                case '11':$nov+=$money;    break;
                                case '12': $dec+=$money;   break;
                            }
                        }
                        $cbs=Db::name('sale_contract_detail')
                            ->alias('scd')
                            ->leftJoin('sale_contract sc','sc.orderNum=scd.saleContractOrderNum')
                            ->where('scd.status',4)->where('scd.type',2)
                            ->whereYear('payOverTime',$item['years'])
                            ->where('saleContractOrderNum','in',$ordernums)
                            ->order('payOverTime ASC')
                            ->field('scd.bjMoney,sc.payOverTime')->select();
                        $jancb=0;
                        $febcb=0;
                        $marcb=0;
                        $aprcb=0;
                        $maycb=0;
                        $juncb=0;
                        $julcb=0;
                        $augcb=0;
                        $sepcb=0;
                        $octcb=0;
                        $novcb=0;
                        $deccb=0;
                        foreach ($cbs as $c){
                            $mon=date('m',strtotime($c['payOverTime']));
                            switch ($mon){
                                case '01': $jancb+=$c['bjMoney'];  break;
                                case '02': $febcb+=$c['bjMoney'];   break;
                                case '03': $marcb+=$c['bjMoney'];   break;
                                case '04': $aprcb+=$c['bjMoney'];   break;
                                case '05': $maycb+=$c['bjMoney'];   break;
                                case '06': $juncb+=$c['bjMoney'];   break;
                                case '07': $julcb+=$c['bjMoney'];   break;
                                case '08': $augcb+=$c['bjMoney'];   break;
                                case '09': $sepcb+=$c['bjMoney'];   break;
                                case '10': $octcb+=$c['bjMoney'];    break;
                                case '11': $novcb+=$c['bjMoney'];    break;
                                case '12': $deccb+=$c['bjMoney'];   break;
                            }
                        }
                        $janll=$jan?($jan-$jancb)/$jan:0;
                        $febll=$feb?($feb-$febcb)/$feb:0;
                        $marll=$mar?($mar-$marcb)/$mar:0;
                        $aprll=$apr?($apr-$aprcb)/$apr:0;
                        $mayll=$may?($may-$maycb)/$may:0;
                        $junll=$jun?($jun-$juncb)/$jun:0;
                        $julll=$jul?($jul-$julcb)/$jul:0;
                        $augll=$aug?($aug-$augcb)/$aug:0;
                        $sepll=$sep?($sep-$sepcb)/$sep:0;
                        $octll=$oct?($oct-$octcb)/$oct:0;
                        $novll=$nov?($nov-$novcb)/$nov:0;
                        $decll=$dec?($dec-$deccb)/$dec:0;

                        $jan=$janll>0.05?round($jan*(0.01+($janll-0.05)*0.8),2):0;
                        $feb=$febll>0.05?round($feb*(0.01+($febll-0.05)*0.8),2):0;
                        $mar=$marll>0.05?round($mar*(0.01+($marll-0.05)*0.8),2):0;
                        $apr=$aprll>0.05?round($apr*(0.01+($aprll-0.05)*0.8),2):0;
                        $may=$mayll>0.05?round($may*(0.01+($mayll-0.05)*0.8),2):0;
                        $jun=$junll>0.05?round($jun*(0.01+($junll-0.05)*0.8),2):0;
                        $jul=$julll>0.05?round($jul*(0.01+($julll-0.05)*0.8),2):0;
                        $aug=$augll>0.05?round($aug*(0.01+($augll-0.05)*0.8),2):0;
                        $sep=$sepll>0.05?round($sep*(0.01+($sepll-0.05)*0.8),2):0;
                        $oct=$octll>0.05?round($oct*(0.01+($octll-0.05)*0.8),2):0;
                        $nov=$novll>0.05?round($nov*(0.01+($novll-0.05)*0.8),2):0;
                        $dec=$decll>0.05?round($dec*(0.01+($decll-0.05)*0.8),2):0;
                    }
                    elseif ($item['position_id']==11){
                        $ordernums=[];
                        $scList=Db::name('sale_contract')->where('status',4)
                            ->where('salesUnitId',$item['userDepartId'])
                            ->where('type',1)
                            ->where('payStatus',3)
                            ->whereYear('payOverTime',$item['years'])
                            ->order('payOverTime ASC')
                            ->field('id,countMoney,orderNum,payOverTime')->select();
                        foreach ($scList as $v){
                            $ordernums[]=$v['orderNum'];
                            $mon=date('m',strtotime($v['payOverTime']));
                            $money=$v['countMoney'];
                            $overed+=$v['countMoney'];
                            switch ($mon){
                                case '01': $jan+=$money;  break;
                                case '02': $feb+=$money;   break;
                                case '03': $mar+=$money;   break;
                                case '04': $apr+=$money;   break;
                                case '05': $may+=$money;   break;
                                case '06': $jun+=$money;   break;
                                case '07': $jul+=$money;   break;
                                case '08': $aug+=$money;   break;
                                case '09': $sep+=$money;   break;
                                case '10':$oct+=$money;    break;
                                case '11':$nov+=$money;    break;
                                case '12': $dec+=$money;   break;
                            }
                        }
                        $jan=round($jan*0.01*0.75,2);
                        $feb=round($feb*0.01*0.75,2);
                        $mar=round($mar*0.01*0.75,2);
                        $apr=round($apr*0.01*0.75,2);
                        $may=round($may*0.01*0.75,2);
                        $jun=round($jun*0.01*0.75,2);
                        $jul=round($jul*0.01*0.75,2);
                        $aug=round($aug*0.01*0.75,2);
                        $sep=round($sep*0.01*0.75,2);
                        $oct=round($oct*0.01*0.75,2);
                        $nov=round($nov*0.01*0.75,2);
                        $dec=round($dec*0.01*0.75,2);
                        $ndhtl=round($overed/$item['dTargets'],2);
                        if ($ndhtl<0.8){
                            $ndjx=0;
                            $nzjx=0;
                        }
                        if (0.8<=$ndhtl&&$ndhtl<=1.5){
                            $ndjx=round($overed*0.1*0.25,2);
                            $cgcb=Db::name('sale_contract_detail')
                                ->where('status',4)->where('type',2)
                                ->where('saleContractOrderNum','in',$ordernums)->sum('bjMoney');
                            $bxjeId=Db::name('expense')->where('admin_id',$item['adminId'])->where('status',5)
                                ->where('contractOrder','in',$ordernums)->column('id');
                            $bxje=Db::name('expense_interfix')->where('orderId','in',$bxjeId)->sum('amount');
                            $nzjx=(($cgcb/$overed)/1.13-$bxje)*0.08;
                            $nzjx=round($nzjx,2);
                        }
                        if ($ndhtl>1.5){
                            $ndjx=round($item['dTargets']*1.5*0.01*0.25,2);
                            $nzjx=round($item['dTargets']*1.5*0.11*0.08,2);
                        }
                        $gljt= BaseService::getGljt($item['adminId'],$item['userDepartId'],$item['position_id'],$item['years']);
                    }
                    elseif ($item['position_id']==12){
                        $ordernums=[];
                        $scList=Db::name('sale_contract')->where('status',4)
                            ->where('saleId',$item['adminId'])
                            ->where('type',1)
                            ->where('payStatus',3)
                            ->whereYear('payOverTime',$item['years'])
                            ->order('payOverTime ASC')
                            ->field('id,countMoney,orderNum,payOverTime')->select();
                        foreach ($scList as $v){
                            $ordernums[]=$v['orderNum'];
                            $mon=date('m',strtotime($v['payOverTime']));
                            $money=$v['countMoney'];
                            $overed+=$v['countMoney'];
                            switch ($mon){
                                case '01': $jan+=$money;  break;
                                case '02': $feb+=$money;   break;
                                case '03': $mar+=$money;   break;
                                case '04': $apr+=$money;   break;
                                case '05': $may+=$money;   break;
                                case '06': $jun+=$money;   break;
                                case '07': $jul+=$money;   break;
                                case '08': $aug+=$money;   break;
                                case '09': $sep+=$money;   break;
                                case '10':$oct+=$money;    break;
                                case '11':$nov+=$money;    break;
                                case '12': $dec+=$money;   break;
                            }
                        }
                        $jan=round($jan*0.01*0.75,2);
                        $feb=round($feb*0.01*0.75,2);
                        $mar=round($mar*0.01*0.75,2);
                        $apr=round($apr*0.01*0.75,2);
                        $may=round($may*0.01*0.75,2);
                        $jun=round($jun*0.01*0.75,2);
                        $jul=round($jul*0.01*0.75,2);
                        $aug=round($aug*0.01*0.75,2);
                        $sep=round($sep*0.01*0.75,2);
                        $oct=round($oct*0.01*0.75,2);
                        $nov=round($nov*0.01*0.75,2);
                        $dec=round($dec*0.01*0.75,2);
                        $ndhtl=round($overed/$item['dTargets'],2);
                        if ($ndhtl<0.8){
                            $ndjx=0;
                            $nzjx=0;
                        }
                        if (0.8<=$ndhtl&&$ndhtl<=1.5){
                            $ndjx=round($overed*0.1*0.25,2);
                            $cgcb=Db::name('sale_contract_detail')
                                ->where('status',4)->where('type',2)
                                ->where('saleContractOrderNum','in',$ordernums)->sum('bjMoney');
                            $bxjeId=Db::name('expense')->where('admin_id',$item['adminId'])->where('status',5)
                                ->where('contractOrder','in',$ordernums)->column('id');
                            $bxje=Db::name('expense_interfix')->where('orderId','in',$bxjeId)->sum('amount');
                            $nzjx=(($cgcb/$overed)/1.13-$bxje)*0.08;
                            $nzjx=round($nzjx,2);
                        }
                        if ($ndhtl>1.5){
                            $ndjx=round($item['dTargets']*1.5*0.01*0.25,2);
                            $nzjx=round($item['dTargets']*1.5*0.11*0.08,2);
                        }

                    }
                    else{

                    }

                    $item['jan']=$jan;
                    $item['feb']=$feb;
                    $item['mar']=$mar;
                    $item['apr']=$apr;
                    $item['may']=$may;
                    $item['jun']=$jun;
                    $item['jul']=$jul;
                    $item['aug']=$aug;
                    $item['sep']=$sep;
                    $item['oct']=$oct;
                    $item['nov']=$nov;
                    $item['dec']=$dec;
                    $item['overed']=$overed;
                    if ($overed &&$item['dTargets']){
                        $overedl=$overed/$item['dTargets'];
                        $overedl=round($overedl*100,2);
                    }
                    $item['overedl']=$overedl;
                    $item['ndjx']=$ndjx;
                    $item['nzjx']=$nzjx;
                    $item['gljt']=$gljt;

                    return $item;
                });
            return table_assign(0, '', $list);
        } else {
            return view();
        }
    }

}